package model

import (
	"fmt"
	"time"
	"yunj/pkg/global"
	"yunj/pkg/yunj/app/enum/stateenum"
	"yunj/pkg/yunj/core/builder/builderdto"
	"yunj/pkg/yunj/util"
)

type AdminPostion struct {
	Id        uint64     `gorm:"primary_key;autoIncrement;column:id" json:"id,omitempty" form:"id"`
	Name      string     `gorm:"column:name;type:varchar(128);not null" json:"name"`
	Code      string     `gorm:"column:code;type:varchar(64);not null;default:''" json:"code"`
	Intro     string     `gorm:"column:intro;type:varchar(255);not null;default:''" json:"intro"`
	Sort      int        `gorm:"column:sort;not null;default:0" json:"sort"`
	CreatedAt *time.Time `gorm:"column:created_at" json:"created_at,omitempty" form:"created_at"`
	UpdatedAt *time.Time `gorm:"column:updated_at" json:"updated_at,omitempty" form:"updated_at"`
	State     uint8      `gorm:"column:state" json:"state" form:"state"`
}

// 获取正常岗位枚举选项
func GetNormalPostionTableColOptions() (options []builderdto.TableColOption, err error) {
	var postions []*AdminPostion
	err = global.MySQL.Model(&AdminPostion{}).Select("id", "name", "code").Where("state = ?", stateenum.NORMAL).Order("sort asc").Find(&postions).Error
	if err != nil {
		err = fmt.Errorf("获取正常岗位枚举选项数据查询异常！%v", err)
		return
	}
	options = make([]builderdto.TableColOption, 0, len(postions))
	for _, postion := range postions {
		label := postion.Name
		if postion.Code != "" {
			label += fmt.Sprintf("(%s)", postion.Code)
		}
		options = append(options, builderdto.TableColOption{
			Value:   postion.Id,
			Label:   label,
			BgColor: util.RandomHexColor("#ffffff"),
		})
	}
	return
}
